/*
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.samples.apps.topeka.helper
import android.app.Activity
import android.content.Intent
import android.content.Context
import android.net.Uri
import com.google.samples.apps.topeka.model.Category
import androidx.core.app.ActivityCompat
import androidx.core.app.ActivityOptionsCompat
import com.google.samples.apps.topeka.activity.SignInActivity
class ActivityLaunchHelper {
	companion object {
		const val EXTRA_EDIT = "EDIT"
		@JvmField public val ACT1: Class<*>? = yfdc.Defapp.getClass("com.google.samples.apps.topeka.activity.CategorySelectionActivity");
		@JvmField public val ACT2: Class<*>? = yfdc.Defapp.getClass("com.google.samples.apps.topeka.activity.QuizActivity");
		@JvmField public val ACT3: Class<*>? = SignInActivity::class.java;
		private const val URL_BASE = "https://topeka.instantappsample.com"
		private const val URL_SIGNIN = "$URL_BASE/signin"
		private const val URL_CATEGORIES = "$URL_BASE/categories"
		private const val URL_QUIZ_BASE = "$URL_BASE/quiz/"
		@JvmStatic fun launchCategorySelection(activity: Activity, options: ActivityOptionsCompat? = null) {
			val starter = categorySelectionIntent(activity)
			if (options == null) {
				activity.startActivity(starter)
			} else {
				ActivityCompat.startActivity(activity, starter, options.toBundle())
			}
		}
		@JvmStatic fun launchSignIn(activity: Activity, edit: Boolean = false) {
			ActivityCompat.startActivity(activity,
					signInIntent(activity, edit),
					ActivityOptionsCompat.makeSceneTransitionAnimation(activity).toBundle())
		}
		@JvmStatic fun categorySelectionIntent(context: Context? = null) = baseIntent(URL_CATEGORIES, context)
		@JvmStatic fun quizIntent(category: Category, context: Context? = null) =
				baseIntent("$URL_QUIZ_BASE${category.id}", context)
		@JvmStatic fun signInIntent(context: Context? = null, edit: Boolean = false): Intent =
				baseIntent(URL_SIGNIN, context).putExtra(EXTRA_EDIT, edit)
		@JvmStatic private fun baseIntent(url: String, context: Context? = null): Intent {
			val yfdc = yfBaseIntent(url, context)
			return if (yfdc != null) {
				yfdc.data = Uri.parse(url)
				yfdc
			} else {
				val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
						.addCategory(Intent.CATEGORY_DEFAULT)
						.addCategory(Intent.CATEGORY_BROWSABLE)
				if (context != null) {
					intent.`package` = context.packageName
				}
				intent
			}
		}
		@JvmStatic private fun yfBaseIntent(url: String, context: Context? = null): Intent? {
			val rtn: Intent = Intent()
			try {
				if (url.startsWith(URL_SIGNIN, true)) {
					if ((context != null) && (context is Activity) && (ACT3 != null)) {
						rtn.setClass(context, ACT3)
					} else {
						rtn.setAction(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER)
						rtn.`package` = "com.google.samples.apps.topeka"
					}
				} else if (url.startsWith(URL_CATEGORIES, true)) {
					if ((context != null) && (context is Activity) && (ACT1 != null)) {
						rtn.setClass(context, ACT1)
					} else {
						rtn.setAction("yfdc.china.CAT_ACTION").addCategory("yfdc.china.CAT_CAT").apply {
							`package` = "com.google.samples.apps.topeka.categories"
						}
					}
				} else if (url.startsWith(URL_QUIZ_BASE, true)) {
					if ((context != null) && (context is Activity) && (ACT2 != null)) {
						rtn.setClass(context, ACT2)
					} else {
						rtn.setAction("yfdc.china.QUIZ_ACT").addCategory("yfdc.china.QUIZ_CAT").apply {
							`package` = "com.google.samples.apps.topeka.quiz"
						}
					}
				} else {
					return null;
				}
			} catch (ex: Throwable) {
				yfdc.Defapp.logException(ex, "YFDC");
				return null;
			}
			return rtn;
		}
	}
}
